function opt=osl_run_opt(opt)

% opt=osl_run_opt(opt)
%
% MWW 2013

opt = osl_check_opt(opt);

opt.results=[];

if(isempty(findstr(opt.dirname, '.opt')))
    opt.dirname=[opt.dirname, '.opt']; 
end;
mkdir(opt.dirname);

% set logfile up
opt.results.logfile=[opt.dirname '/log-' date '.txt'];
% delete any existing diary file with the same name
runcmd(['rm -f ' opt.results.logfile]);

diary(opt.results.logfile);

opt.results.date=date;

% set first level diagnostic report up    
opt.results.plotsdir=[opt.dirname '/plots'];
opt_report=osl_report_setup(opt.results.plotsdir,['OPT report'],opt.results.logfile);  

spm_files_basenames={};
spm_files_epoched_basenames={};

if(isempty(opt.sessions_to_do) || length(opt.sessions_to_do)==0)
    error('There are no sessions to analyse');
end;

if opt.maxfilter.do && ~strcmp(opt.input_file_type,'raw_fif_files'),
    error('Input files must be raw file files to do Maxfiltering. Consider changing opt.maxfilter.do to 0.');
end;
if ~opt.maxfilter.do && strcmp(opt.input_file_type,'raw_fif_files'),
    error('Input files can not be raw fif files if NOT Maxfiltering. Consider changing opt.maxfilter.do to 1.');    
end;

for subi=1:length(opt.sessions_to_do), 
    subnum=opt.sessions_to_do(subi);
    
    switch opt.input_file_type
    case 'raw_fif_files'
        input_file=opt.raw_fif_files{subnum};
    case 'input_files'   
        input_file=opt.input_files{subnum};   
    case 'spm_files'
        input_file=opt.spm_files{subnum}
    otherwise
        error('Invalid input_file type');    
    end;
    
    disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
    disp(['%%%%%%%%%%%%%%%%%%%%%%%  RUNNING OPT ON SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
    disp(['%%%%%%%%%%%%%%%%%%%%%%%  Input file: ' input_file '  %%%%%%%%%%%%%%%%%%%%%%%'])
                    
    % set session specific diagnostic report up    
    report_dir=[opt.results.plotsdir '/' opt.results.date '_sess' num2str(subnum)];
    report=osl_report_setup(report_dir,['Session ' num2str(subnum) ' (Input file:' input_file  ')']);           
  
    if(opt.maxfilter.do)
        
        %if(~opt.maxfilter.do_sss_maxfilter ||  (opt.maxfilter.do_remove_badchans_pre_sss && opt.maxfilter.do_sss_maxfilter))
            
        %%%%%%%%%%%%%%%%%%
        %% Create 1st round of non-SSSed data if doing double Maxfilter
        % OR create final non-SSSed Maxfiltered data if not doing SSS
        disp(['%%%%%%%%%%%%%%%%%%%%%%%  MAXFILT WITH NO SSS, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
        Smf=[];
        [p fifname e] = fileparts(opt.raw_fif_files{subnum});       
        Smf.fif=[p '/' fifname];
        Smf.fif_out=[opt.dirname '/' fifname '_nosss'];
        Smf.logfile=1;

        if ~(exist([Smf.fif '.fif'],'file')==2),
            error(['opt.raw_fif_files{' num2str(subnum) '} file ' [Smf.fif '.fif'] ' does not exist']);
        end;
        
        Smf.movement_compensation=0;
                
        if(~Smf.movement_compensation)
            disp('Assuming data was sampled at 1000 Hz');
            sample_freq=1000;
            if(opt.downsample.freq<=sample_freq/4 && opt.downsample.do)
                Smf.downsample_factor=4;
            elseif(opt.downsample.freq<=sample_freq/2 && opt.downsample.do)
                Smf.downsample_factor=2;
            else
                Smf.downsample_factor=1;
            end;
        end;                    
        
        Smf.nosss=1;
        fif_sss=osl_call_maxfilter(Smf);
        runcmd(['cat ' fif_sss '_log.txt']);
        
        if(~exist([fif_sss '.fif'],'file'))
            warning([fif_sss '.fif does not exist. Can not continue with this subject.']);
            break;                    
        end;

        %end;

        %%%%%%%%%%%%%%%%%%%
        %% Convert to SPM

        S2=[];
        disp(['%%%%%%%%%%%%%%%%%%%%%%%  CONVERT, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
        spm_files_basenames{subnum}=['nosss' opt.spm_files_basenames{subnum}];
        spm_file=[opt.dirname '/' spm_files_basenames{subnum}];
        S2.spm_file=spm_file;
        [p fifname e] = fileparts(Smf.fif_out);       
        S2.fif_file=[p '/' fifname '.fif'];
        if(isfield(opt,'trigger_channel_mask'))
            S2.trigger_channel_mask=opt.trigger_channel_mask;
        end;
        
        [D spm_files{subnum}] = osl_convert_script(S2);

        % delete fif file in opt dir that is no longer needed
        if(opt.cleanup_files)
            runcmd(['rm -f ' S2.fif_file]);
        end;
        
        close all

        %%%%%%%%%%%%%%%%%%%
        %%
        if(opt.maxfilter.do_sss)

            if(opt.maxfilter.do_remove_badchans_pre_sss),
                
                %%%%%%%%%%%%%%%%%%%
                %% Do DOUBLE Maxfilter procedure:
                %% First detects and remove bad channels on non-SSS data,
                %% then runs SSS            
                disp(['%%%%%%%%%%%%%%%%%%%%%%%  RM BAD CHANS, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
                spm_file=[opt.dirname '/' spm_files_basenames{subnum}];
                D=spm_eeg_load(spm_file);

                for ii=1:length(opt.modalities),
                    printprefix_mod=[opt.modalities{ii} '_preSSS'];

                    %%%%%%%%%%%%%%%%%%%%%
                    % use outlier detection approach
                    if(1),

                        S = [];
                        S.D = D;
                        S.modalities{1}=opt.modalities{ii};
                        S.do_plot=0;   
                        S.max_iter=1;
                        %S.outlier_measure_fns={'min','std'};
                        S.outlier_measure_fns={'std'};
                        S.outlier_wthresh_chan=[0.01];
                        S.just_chans=1;
                        [D2]=osl_detect_badevent_v2(S);

                        list_bch=badchannels(D2); 

                        if(~isempty(list_bch))
                            bad_list=badchannels(D);
                            vect_bch=zeros(1,size(D,1));
                            vect_bch(bad_list)=1;
                            vect_bch(list_bch)=1;

                            D=badchannels(D,[],vect_bch);
                            D.save;
                        end;
                    
                        % delete obsolete spm file
                        spm_file_old=[opt.dirname '/S' spm_files_basenames{subnum}];
                        Dold=spm_eeg_load(spm_file_old);
                        
                        if(opt.cleanup_files)
                            Dold.delete;
                        end;
                        
                        %%%%%%%%%%%%%
                        % diagnostic plots
                        S=[];
                        S.D=D;
                        S.print_plots=1;
                        S.plotsdir=opt.results.plotsdir;
                        S.modality=opt.modalities{ii};
                        S.printprefix=[printprefix_mod '_outlier'];
                        [res fig_names fig_handles]=osl_check_bad_chans(S);

                        report=osl_report_set_figs(report,fig_names,fig_handles);
                        report=osl_report_print_figs(report);
           
                        %%%%%%%%%%%%%%%
                    end;
                    %%%%%%%%%%%%%%%%%%%%%

                    opt.results.badchans_pre_sss{subnum,ii}=badchannels(D);

                    D.save;

                end;
            end;

            %%%%%%%%%%%%%%%%%%%
            %% Re-Maxfilter data using bad channel information

            disp(['%%%%%%%%%%%%%%%%%%%%%%%  MAXFILT WITH SSS, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
            Smf=[];
            [p fifname e] = fileparts(opt.raw_fif_files{subnum});       
            
            Smf.fif=[p '/' fifname];
            Smf.fif_out=[opt.dirname '/' fifname '_sss'];  
                        
            Smf.logfile=1;
            Smf.nosss=0;

            spm_file=[opt.dirname '/' spm_files_basenames{subnum}];

            Smf.spmfile=spm_file;
            Smf.movement_compensation=opt.maxfilter.movement_compensation; %can only run with sss AND downsampling has to be switched off
            
            if(opt.downsample.do && Smf.movement_compensation)
                disp('Can not do any downsampling during Maxfilter call when Smf.movement_compensation is on.');
            end;
            
            if(~Smf.movement_compensation)
                disp('Assuming data was sampled at 1000 Hz');
                sample_freq=1000;
                if(opt.downsample.freq<=sample_freq/4 && opt.downsample.do)
                    Smf.downsample_factor=4;
                elseif(opt.downsample.freq<=sample_freq/2 && opt.downsample.do)
                    Smf.downsample_factor=2;
                else
                    Smf.downsample_factor=1;
                end;
            end;
            
            fif_sss=osl_call_maxfilter(Smf);
            runcmd(['cat ' fif_sss '_log.txt']);

            opt.results.sss_autobad_off(subnum)=0;
            
            if(~exist([fif_sss '.fif'],'file'))
                warning('Maxfilter has FAILED. Will retry with no bad channels set and autobad off!!! Results may be suspect.');
                
                opt.results.sss_autobad_off(subnum)=1;
                % set all chans good
                D=spm_eeg_load(spm_file);
                chans=D.meegchannels;
                D = badchannels(D,chans,zeros(size(chans)));
                save(D);
        
                Smf.autobad_off=1;
                fif_sss=osl_call_maxfilter(Smf);
                runcmd(['cat ' fif_sss '_log.txt']);

                if(~exist([fif_sss '.fif'],'file'))
                    warning('Maxfilter has still FAILED. Can not continue with this subject.');
                    break;                    
                end;
            end;
            
            %%%%%%%%%%%%%%%%%%%
            %% Convert to SPM

            disp(['%%%%%%%%%%%%%%%%%%%%%%%  CONVERT SSS-ed DATA, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])

            S2=[];

            % delete obsolete spm file
            spm_file_old=[opt.dirname '/' spm_files_basenames{subnum}];        
            Dold=spm_eeg_load(spm_file_old);
            if(opt.cleanup_files)
                Dold.delete;
            end;
            
            spm_files_basenames{subnum}=['sss' opt.spm_files_basenames{subnum}];
            spm_file=[opt.dirname '/' spm_files_basenames{subnum}];
            S2.spm_file=spm_file;
            [p fifname e] = fileparts(Smf.fif_out);       
            S2.fif_file=[p '/' fifname '.fif'];
        
            if(isfield(opt,'trigger_channel_mask'))
                S2.trigger_channel_mask=opt.trigger_channel_mask;
            end;
            [D spm_files{subnum}, fig_handles, fig_names ] = osl_convert_script(S2);
            
            if ~isempty(fig_handles),
                report=osl_report_set_figs(report,fig_names,fig_handles);
                report=osl_report_print_figs(report);
            end;
            
            % delete fif file in opt dir that is no longer needed
            if(opt.cleanup_files)
                runcmd(['rm -f ' S2.fif_file]);
            end;
        end;        

    else,
        
        if(~isempty(opt.input_files)),
            % Skipping Maxfilter calls - assume these have already been run, 
            % or we are not working with Neuromag data, 
            % and so go straight for convert call using opt.input_files

            %%%%%%%%%%%%%%%%%%%
            %% Convert to SPM
            disp(['%%%%%%%%%%%%%%%%%%%%%%%  CONVERT INPUT DATA, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])

            S2=[];

            [p spmname e] = fileparts(opt.spm_files_basenames{subnum});                               
            spm_files_basenames{subnum}=[spmname '.mat'];           
            S2.spm_file=[opt.dirname '/' spm_files_basenames{subnum}];

            S2.fif_file=[opt.input_files{subnum}];

            if(isfield(opt,'trigger_channel_mask'))
                S2.trigger_channel_mask=opt.trigger_channel_mask;
            end;
            [D spm_files{subnum}, fig_handles, fig_names ] = osl_convert_script(S2);
            
            if ~isempty(fig_handles),
                report=osl_report_set_figs(report,fig_names,fig_handles);
                report=osl_report_print_figs(report);
            end;
           
            close all
        else
            
            % use passed in SPM MEEG objects
            cd(opt.dirname);
            
            S2=[];
            [p fname e] = fileparts(opt.spm_files{subnum}); 
            S2.D=[p '/' fname '.mat'];
            
            tempstring = tempname;
            tempstring = tempstring(end-12:end);
            S2.newname=[spm_files_basenames{subnum} tempstring];
            [p spmname e] = fileparts(S2.newname);                               
            S2.newname=[spmname '.mat']; 

            S2.updatehistory=0;
            D = spm_eeg_copy(S2);
            
            runcmd(['mv ' D.path '/' D.fname ' ' opt.dirname]);
            runcmd(['mv ' D.path '/' D.fnamedat ' ' opt.dirname]);
            spm_filename=[opt.dirname '/' D.fname];
            D=spm_eeg_load(spm_filename);

        end;
        
    end;    
   
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Downsample with SPM (particularly important if movement compensation used - but worth doing anyway)

    if(opt.downsample.do),
        S=[];
        disp(['%%%%%%%%%%%%%%%%%%%%%%%  DOWNSAMP, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
        spm_file=[opt.dirname '/' spm_files_basenames{subnum}];
        S.D=spm_file;
        S.fsample_new = opt.downsample.freq;
        D = spm_eeg_downsample (S);

        % delete obsolete spm file
        spm_file_old=[opt.dirname '/' spm_files_basenames{subnum}];        
        Dold=spm_eeg_load(spm_file_old);
        if(opt.cleanup_files)
            Dold.delete;
        end;

        spm_files_basenames{subnum}=['d' spm_files_basenames{subnum}];

        close all
    end;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Perform AfRICA - ICA denoising

    if(opt.africa.do)
            
        disp(['%%%%%%%%%%%%%%%%%%%%%%%  AFRICA, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
        S=opt.africa;
        
        spm_file=[opt.dirname '/' spm_files_basenames{subnum}];
        S.fname=spm_file;                    
        S.logfile = 0;                                 
        S.ica_file = [S.fname '_preproc_ica_results'];           
        S.do_plots=1;                                          
        S.just_ica=0;       % set to 1 if want to run ICA only and not other two stages as well  

        S.to_do=[1 1 1];    %  here define which ICA stage to be run 1 = ICA only, 2 = component classification, 3 = component removal 
                 
        [spm_files_new{subnum}, fig_handles, fig_names, S]=osl_africa(S);
        opt.results.africa{subnum}=S.ica_res;
        
        report=osl_report_set_figs(report,fig_names,fig_handles);
        report=osl_report_print_figs(report);   
        
        % delete obsolete spm file
        spm_file_old=[opt.dirname '/' spm_files_basenames{subnum}];        
        Dold=spm_eeg_load(spm_file_old);
        if(opt.cleanup_files)
            Dold.delete;
        end;

        spm_files_basenames{subnum}=['A' spm_files_basenames{subnum}];
    end;
    
    %%%%%%%%%%%%%%%%%%%
    %% High Pass Filter
    % To get rid of low frequency fluctuations in the data

    if(opt.highpass.do)
        disp(['%%%%%%%%%%%%%%%%%%%%%%%  HP FILT, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
        spm_file=[opt.dirname '/' spm_files_basenames{subnum}];
        S=[];
        S.D = spm_file;
        S.filter.band='high';
        S.filter.PHz=opt.highpass.cutoff;
        S.filter.dir='twopass';
        D = spm_eeg_filter_v2(S);

        % delete obsolete spm file
        spm_file_old=[opt.dirname '/' spm_files_basenames{subnum}];        
        Dold=spm_eeg_load(spm_file_old);
        if(opt.cleanup_files)
            Dold.delete;
        end;

        spm_files_basenames{subnum}=['f' spm_files_basenames{subnum}];
    end;

    %%%%%%%%%%%%%%%%%%%
    %% DO REGISTRATION AND RUN FORWARD MODEL BASED ON STRUCTURAL SCANS

    if(opt.coreg.do),
        disp(['%%%%%%%%%%%%%%%%%%%%%%%  COREG, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])
        S=[];
        spm_file=[opt.dirname '/' spm_files_basenames{subnum} '.mat'];
        S.D = [spm_file];
        S.mri=opt.coreg.mri{subnum};
        S.useheadshape=opt.coreg.useheadshape;    
        S.forward_meg = 'MEG Local Spheres';
        S.forward_meg = 'Single Shell';

        if(isfield(opt.coreg,'fid_mnicoords')),
            S.fid_mnicoords=opt.coreg.fid_mnicoords; 
            % flirt -in /Users/woolrich/Desktop/GN170_anatomy_test.nii -ref /usr/local/fsl/data/standard/MNI152_T1_2mm -out /Users/woolrich/Desktop/anat_mne2;
        end;
        S.fid_label=opt.coreg.fid_label

        D=osl_forward_model(S);
        clc
        close all;

        % CHECK REGISTRATION

        % mnifid = ft_transform_headshape(D.inv{1}.datareg.toMNI,D.inv{1}.datareg.fid_mri );mnifid.fid.pnt 
        % opt.fid_mnicoords.nasion =[  1.3968   81.9389  -44.9899];opt.fid_mnicoords.lpa =[-83.3246  -20.2372  -68.1528];opt.fid_mnicoords.rpa = [83.9906  -19.5985  -65.6612];

        %%
        spm_file=[opt.dirname '/' spm_files_basenames{subnum} '.mat'];
        Dcheck=spm_eeg_load(spm_file);
        %spm_eeg_inv_checkdatareg(D);    

        coregfig1 = sfigure;
        set(coregfig1,'Position',[300 300 1200 800]);
        set(coregfig1,'Color', [1,1,1]);
        subplot(1,3,1)
        spm_eeg_inv_checkdatareg_3Donly(Dcheck);
        view(-180,0)
        %title(['concatMefsession' num2str(counter) '_spm_meeg'])
        subplot(1,3,2)
        spm_eeg_inv_checkdatareg_3Donly(Dcheck);
        view(-270,0)
        subplot(1,3,3)
        spm_eeg_inv_checkdatareg_3Donly(Dcheck);
        view(130,18)
        title(['Session ' num2str(subnum)]);
        %%

        report=osl_report_set_figs(report,'Coregistration',coregfig1);
        report=osl_report_print_figs(report);
    end;
    
    %%%%%%%%%%%%%%%%%%%
    %% DO EPOCHING (if epoch-based task data)
        
    if(opt.epoch.do),
        % DO preliminary epoching for the purpose of finding outliers
        % This is not the final epoching. Instead this sets up the epoch
        % definitions, and performs a temporary epoching for the purpose of doing
        % semi-automated outlier trial rejection (before running the fully 
        % automated OAT).
        %
        % The epoch definitions and the continuous data will be kept and
        % passed into OAT. This is so that things like temporal filtering (which is
        % dones as part of OAT) can be done on the continuous data, before the data
        % is epoched inside OAT.
        %
        % Note that this will also remove those trials that overlap with the bad
        % epochs identified using oslview.

        disp(['%%%%%%%%%%%%%%%%%%%%%%%  EPOCH, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])

        %%%%
        % define the trials we want from the event information
        S2 = [];
        spm_file=[opt.dirname '/' spm_files_basenames{subnum}];

        S2.D = spm_file;
        D_continuous=spm_eeg_load(S2.D);

        S2.pretrig = opt.epoch.time_range(1)*1000; % epoch start in ms
        S2.posttrig = opt.epoch.time_range(2)*1000; % epoch end in ms   

        S2.trialdef=opt.epoch.trialdef;
        S2.reviewtrials = 0;
        S2.save = 0;
        S2.epochinfo.padding = 0;
        S2.event = D_continuous.events;
        S2.fsample = D_continuous.fsample;
        S2.timeonset = D_continuous.timeonset;

        [epochinfo.trl, epochinfo.conditionlabels, S3] = spm_eeg_definetrial(S2);    

        %%%%
        % adjust timings to account for delay between trigger and visual display 
        if(opt.epoch.timing_delay~=0)
            timing_delay=opt.epoch.timing_delay; % secs
            epochinfo_new=epochinfo;
            epochinfo_new.trl(:,1:2)=epochinfo.trl(:,1:2)+round(timing_delay/(1/D_continuous.fsample));
            epochinfo=epochinfo_new;
        end;

        %%%%
        % do epoching
        S3=[];
        S3.D = D_continuous;     
        S3.epochinfo=epochinfo;
        [D good_trial_start_times] = osl_epoch(S3);

        spm_files_epoched_basenames{subnum}=['e' spm_files_basenames{subnum}];

        %%%%%%%%%%%%%%%%%%%%
        %% Detect bad events and chans

        disp(['%%%%%%%%%%%%%%%%%%%%%%%  BAD CHAN/EVENTS, SESS = ' num2str(subnum) '  %%%%%%%%%%%%%%%%%%%%%%%'])

        spm_file=[opt.dirname '/' spm_files_epoched_basenames{subnum}];
        D2=spm_eeg_load(spm_file);

        S = [];
        S.D = D2;
        S.modalities=opt.modalities;
        S.do_plot=1;   
        S.max_iter=5;
        S.outlier_measure_fns=opt.outliers.outlier_measure_fns;%{'min','std'};
        S.outlier_wthresh_ev=opt.outliers.wthresh_ev;%[0.5 0.5];
        S.outlier_wthresh_chan=opt.outliers.wthresh_chan;%[0.01 0.01];
        S.just_chans=0;

        [D2 fig_names fig_handles]=osl_detect_badevent_v2(S);
        
        report=osl_report_set_figs(report,fig_names,fig_handles);
        report=osl_report_print_figs(report);
        
        % delete obsolete spm file
        spm_file_old=[opt.dirname '/' spm_files_epoched_basenames{subnum}];        
        Dold=spm_eeg_load(spm_file_old);
        if(opt.cleanup_files)
            Dold.delete;
        end;

        spm_files_epoched_basenames{subnum}=['S' spm_files_epoched_basenames{subnum}];

    end;
    
    %%%%%%%%%%%%%%%%%%%
    %% generate source recon web report for this session
    report=osl_report_write(report);        
    opt_report=osl_report_add_sub_report(opt_report, report)
        
end;

%%%%%%%%%%%%%%%%%%%%
%% diagnostic plots over all sessions

try,
    for subi=1:length(opt.sessions_to_do), subnum=opt.sessions_to_do(subi);
        S2=[];
        spm_file=[opt.dirname '/' spm_files_epoched_basenames{subnum}];

        S2.D = spm_file;   
        D=spm_eeg_load(S2.D);

        badchans(subi)=sum(D.badchannels>0);
        rejected(subi)=sum(D.reject);
        
        if isfield(opt.results,'sss_autobad_off')        
            autobadoff(subi)=opt.results.sss_autobad_off(subnum);
        end;
        
        if isfield(opt.results,'africa')
            icsremoved(subi)=length(opt.results.africa{subnum}.bad_components);
        end;
    end;

    opt_report=osl_report_set_figs(opt_report,'num_reject_trials');
    plot(opt.sessions_to_do,rejected,'*');xlabel('sess no.');ylabel('Num trials rejected'); % plot the number of rejected trials        
    opt_report=osl_report_print_figs(opt_report);

    opt_report=osl_report_set_figs(opt_report,'num_bad_chans');
    plot(opt.sessions_to_do,badchans,'*');xlabel('sess no.');ylabel('Num channels rejected'); % plot the number of rejected channels
    opt_report=osl_report_print_figs(opt_report);

    if isfield(opt.results,'sss_autobad_off')
        opt_report=osl_report_set_figs(opt_report,'sss_autobad_off');
        plot(opt.sessions_to_do,autobadoff,'*');xlabel('sess no.');ylabel('SSS autobad off flag'); % plot the number of rejected channels
        opt_report=osl_report_print_figs(opt_report);
    end;
        
    if isfield(opt.results,'africa')
        opt_report=osl_report_set_figs(opt_report,'africa_ics_removed');
        plot(opt.sessions_to_do,icsremoved,'*');xlabel('sess no.');ylabel('Num ICs removed by AFRICA'); % plot the number of rejected channels
        opt_report=osl_report_print_figs(opt_report);
    end;
    
catch,
end;

%%%%%%%%%%%%%%%%%%%
%% generate web report
opt.results.report=osl_report_write(opt_report);        

%%%%%%%%%%%%%%%%%%%
%% output res
opt.osl_version=osl_version;

opt.results.spm_files_basenames=spm_files_basenames;
opt.results.spm_files_epoched_basenames=spm_files_epoched_basenames;

opt.results.spm_files=[];
opt.results.spm_files_epoched=[];
if ~isempty(opt.results.spm_files_basenames),
    for subi=1:length(opt.sessions_to_do), subnum=opt.sessions_to_do(subi);
        opt.results.spm_files{subnum}=[opt.dirname '/' opt.results.spm_files_basenames{subnum} '.mat'];

        if(opt.epoch.do),
            opt.results.spm_files_epoched{subnum}=[opt.dirname '/' opt.results.spm_files_epoched_basenames{subnum} '.mat'];
        end;
    end;
end;

opt.fname=[opt.dirname '/opt-' opt.results.date];

save(opt.fname, 'opt');
 
disp(['To view OPT report, point your browser to <a href="' opt.results.report.html_fname '">' opt.results.report.html_fname '</a>']);

diary off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% OPTIONAL EXTRAS BELOW:

%%%%%%%%%%%%%%%%%%%
%% Run this bit of code if you want to unreject all trials and channels
if(0)
    for subi= 1:length(spm_files), subnum=opt.sessions_to_do(subi);
        S2=[];
        spmfilename=spm_files{subnum};
        S2.D = spmfilename;        
        D=spm_eeg_load(S2.D);

        rejected=sum(D.reject)
        tmp=1:length(D.conditions);
        trlsel = zeros(1, length(tmp));
        D = reject(D, tmp, trlsel); 
        rejected=sum(D.reject)

        chans=D.meegchannels;
        D = badchannels(D,chans,zeros(size(chans)));
        save(D);
    end;
end;
%%%%%%%%

